package com.guigu.www.cxk.service.imp;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.guigu.www.cxk.service.CXKChatTableService;
import com.guigu.www.mapper.ChatTableMapper;
import com.guigu.www.mapper.ConsultMessagesMapper;
import com.guigu.www.mapper.HealthManagerMapper;
import com.guigu.www.mapper.MemberMapper;
import com.guigu.www.pojo.ChatTable;
import com.guigu.www.pojo.ConsultMessages;
import com.guigu.www.pojo.HealthManager;
import com.guigu.www.pojo.Member;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@Service
public class CXKChatTableServiceImpl implements CXKChatTableService {
    @Autowired
    private ChatTableMapper chatTableMapper;
    @Autowired
    private ConsultMessagesMapper consultMessagesMapper;
    @Autowired
    private HealthManagerMapper healthManagerMapper;
    @Autowired
    private MemberMapper memberMapper;

    @Override
    public List<ChatTable> initChatTableList(Integer memId) {
        QueryWrapper<ChatTable> wrapper = new QueryWrapper<>();
        wrapper.eq("mem_id",memId);
        wrapper.orderByDesc("chat_ta_create_time");
        List<ChatTable> chatTables = chatTableMapper.selectList(wrapper);
        for (ChatTable chatTable : chatTables) {
            Member member = memberMapper.selectById(chatTable.getMemId());
            HealthManager healthManager = healthManagerMapper.selectById(chatTable.getHmId());

            QueryWrapper<ConsultMessages> conWrapper = new QueryWrapper<>();
            conWrapper.eq("chat_ta_id", chatTable.getChatTaId());
            List<ConsultMessages> consultMessages = consultMessagesMapper.selectList(conWrapper);

            conWrapper.eq("con_mes_mem_watch",0);
            Integer count = consultMessagesMapper.selectCount(conWrapper);
            chatTable.setMember(member);
            chatTable.setHealthManager(healthManager);
            chatTable.setMessagesList(consultMessages);
            chatTable.setNoWatchCount(count);
        }

        // 返回数据集合
        return chatTables;
    }

    @Override
    public Integer upConsultHmWatch(Integer chatTaId) {
        QueryWrapper<ConsultMessages> wrapper = new QueryWrapper<>();
        wrapper.eq("chat_ta_id",chatTaId);
        ConsultMessages consultMessages = new ConsultMessages();
        consultMessages.setConMesMemWatch(1);
        return consultMessagesMapper.update(consultMessages,wrapper);
    }

    @Override
    public Integer addConsultMsg(Integer chatTaId, Integer conMesType, String conMesContent, Integer conMesState, String conMesAccount) {
        ChatTable chatTable = new ChatTable();
        chatTable.setChatTaId(chatTaId);
        chatTable.setChatTaState(0);
        chatTableMapper.updateById(chatTable);
        // 获取当前时间
        Date now = new Date();
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String time = dateFormat.format( now );
        ConsultMessages con = new ConsultMessages(null,chatTaId,conMesContent,conMesType,time,0,1,conMesAccount,conMesState);
        // System.out.println(con);
        return consultMessagesMapper.insert(con);
    }

    @Override
    public List<ChatTable> SelectTable(int mid) {
        QueryWrapper<ChatTable> wrapper = new QueryWrapper<>();
        wrapper.eq("mem_id",mid);
        return chatTableMapper.selectList(wrapper);
    }
}
